<template>
  <div class="app-container" v-loading="loading">
    <div class="hd">
        <p>*财大教工请输工号，外校教师请输身份证号(身份证最后一位如果是X请使用大写)</p>
    </div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="工号/身份证号" prop="username">
        <el-input v-model="form.username" ></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" ></el-input>
      </el-form-item>
      <center>
          <el-button type="primary" @click="onSubmit('form')">提交</el-button>
          <el-button type="primary"><router-link to="/">回首页</router-link></el-button>
      </center>
    </el-form>
  </div>
</template>

<script>
import { sendMessage } from '@/api/basicInfoSetting'
import md5 from 'js-md5'
import axios from 'axios'
export default {
  name: 'findPassword',
  data() {
    var validateMobile = (rule, value, callback) => {
      var pattern = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else if (!pattern.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      form: {
        mobile: '',
        username: ''
      },
      rules: {
        mobile: [
          { required: true, validator: validateMobile, trigger: 'blur' }
        ],
        username: [
          { required: true, trigger: 'blur', message: '请输入工号或身份证号' }
        ]
      },
      loading: false
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          const newPassword = Math.random().toString(16).substr(2, 6)
          const formData = new FormData()
          formData.append('username', this.form.username)
          formData.append('mobile', this.form.mobile)
          formData.append('newPassword', md5(newPassword))
          axios.post('/api/teachers/findPassword', formData).then((response) => {
            if (response.data.code === '2000') {
              this.loading = false
              const sendParam = {
                mobile: this.form.mobile,
                message: '您的新密码是：' + newPassword + '请登录系统后及时修改'
              }
              sendMessage(sendParam.mobile, sendParam.message).then(response => {
              }).catch(error => {
                console.log(error)
                this.$message.error('短信发送失败,请稍后重试')
              })
              this.$router.push({ path: '/' })
            } else if (response.data.code === '3000') {
              this.loading = false
              this.$message.error('您输入的手机号和系统注册的手机号不一致，请联系管理员')
            } else {
              this.loading = false
              this.$message.error('找回密码失败请联系管理员')
            }
          }).catch(() => {
            this.loading = false
            this.$message.error('找回密码失败请联系管理员')
          })
        } else {
          console.log('error')
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.line{
  text-align: center;
}
.hd p {
  font-size: 15px;
  text-align: center;
  color: red;
}
</style>

